package com.zhida.common.interceptor;

import com.zhida.common.mapper.RolePermissionMapper;
import com.zhida.common.utils.ObjectUtil;
import jakarta.annotation.Resource;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;

import static com.zhida.common.utils.StaticKeyUtil.USER_ROLE_KEY;

/**
 * Create by zhh 2024/7/21 13:05
 */
@Component
public class RoleInterceptor implements CommandLineRunner {

    @Resource
    private RolePermissionMapper rolePermissionMapper;
    @Resource
    RedisTemplate<String,String> redisTemplate;

    @Override
    public void run(String... args) throws Exception {
        //  从数据库中读取用户权限数据
        ArrayList<HashMap<String, String>> methodsLevel = rolePermissionMapper.getMethodsLevel();
        //  将用户权限数据存入缓存
        redisTemplate.opsForValue().set(USER_ROLE_KEY, ObjectUtil.Object2JsonStr(methodsLevel));
        System.out.println("用户权限数据存入缓存成功");
    }
}
